package service.serviceimpl;


import basedao.BaseDaoProperties;
import entity.T163;
import service.Service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author wuchao66
 * @Date 2023/3/28 13:57
 * @PackageName:com.wuchao.jdbc2.service.serviceimpl
 * @ClassName: ServiceImpl
 * @Description: TODO
 * @Param
 * @Version 1.0
 */
public class ServiceImpl extends BaseDaoProperties implements Service {
    @Override
    public List<T163> executeQuery(String sql, Object... xjj) {
        Connection connection=getConnection();
        ResultSet resultSet=null;
        PreparedStatement preparedStatement=null;
        List<T163> list=new ArrayList<>();
        try {
            preparedStatement=connection.prepareStatement(sql);
            for(int i=0;i<xjj.length;i++){
                preparedStatement.setObject((i+1),xjj[i]);
            }
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                T163 t163 = new T163();
               t163.id= resultSet.getInt(1);
                t163.name= resultSet.getString(2);
               t163.sex= resultSet.getString(3);
               t163.pwd= resultSet.getString(4);
               list.add(t163);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            closeAll(connection,preparedStatement,resultSet,null);
        }

        return list;
    }

    @Override
    public int executeUpdate(String sql, Object... xjj) {
        Connection connection=getConnection();
       int num=0;
        PreparedStatement preparedStatement=null;
        try {
            preparedStatement=connection.prepareStatement(sql);
            for(int i=0;i<xjj.length;i++){
                preparedStatement.setObject((i+1),xjj[i]);
            }
            num = preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            closeAll(connection,preparedStatement,null,null);
        }
        return num;

    }

    @Override
    public int load(String sql, Object... xjj) {
        Connection connection=getConnection();
        ResultSet resultSet=null;
        PreparedStatement preparedStatement=null;
        int num=0;
        try {
            preparedStatement=connection.prepareStatement(sql);
            for(int i=0;i<xjj.length;i++){
                preparedStatement.setObject((i+1),xjj[i]);
            }
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                num=resultSet.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            closeAll(connection,preparedStatement,resultSet,null);
        }
        return num;
    }
}
